/*
 * @Author: xushuhao
 * @Email: xshmouse@163.com
 * @Date: 2020-04-23 09:06:42
 * @Last Modified by: xushuhao,高富强
 * @Last Modified time: 2020-06-03 09:38:09
 * @Description: 班级页面 - 列表 - TabBar导航
 */


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_code_framework/base/base_widget.dart';
import 'package:flutter_code_framework/model_data/class/class.dart';
import 'package:flutter_code_framework/pages/class/class_list_pop_page.dart';
import 'package:flutter_code_framework/route/route_transition.dart';
import 'package:flutter_code_framework/utils/global.dart';
import 'package:flutter_sfsymbols/flutter_sfsymbols.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:flutter_code_framework/pages/class/class_info_page.dart';
import 'package:flutter_code_framework/pages/common/search.dart';

class ClassroomPage extends BaseWidget {
  ClassroomPage({Key key}) : super(key: key);
  
  // final Map args;

  //ClassroomPage({this.args});

  @override
  BaseWidgetState<BaseWidget> getState() {
    return _ClassroomPageState();
  }
}

class _ClassroomPageState extends BaseWidgetState<ClassroomPage> {
  Map args;
  int curPage = 1;
  int pageNum = 20;
  int countflag=1;//加载进度是否显示用
  ScrollController _controller = ScrollController();


  List<Object> listData = [null];
  SlidableController slidableController;
  bool isListViewItemDel = false;
  Animation<double> _rotationAnimation;
  @override
  Widget buildWidget(BuildContext context) {
    bool isEmpty=false;
    if(listData.length<10||listData==[null] )
    {
       isEmpty=true;
    }
    return
      Container(

        height: getMainWidgetHeight() - kTabbarHeigt,
        child: ListView(

        //crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[

            Container(
              height:70,
              margin: EdgeInsets.only(left:24),
              child:
              IntrinsicHeight(
                child:
                Row(
                  children: <Widget>[
                  Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                     children: <Widget>[
                          Container(
                           height: 30,
                            //color: Colors.red,
                            //padding: EdgeInsets.only(bottom:0),
                            child:
                            Text('班级',
                              style: TextStyle(
                                color:  Theme.of(context).primaryTextTheme.caption.color,
                                fontSize: 22.0,
                                height:1.1,
                            ),
                              //overflow:TextOverflow.visible ,
                            ),
                            alignment: Alignment.bottomLeft,
                          ),
                          Container(
                            height: 30,
                            //color: Colors.red,
                            margin: EdgeInsets.only(left:5,bottom:2),
                            child: Text('|',
                                //textAlign:TextAlign.end,
                                style: TextStyle(
                                color: Theme.of(context).primaryTextTheme.caption.color.withOpacity(0.5),
                                fontSize: 13.0,
                                height:1.1
                            )),
                            alignment:  Alignment.bottomLeft,
                          ),
                          Container(
                            height: 30,
                            //color: Colors.red,
                            margin: EdgeInsets.only(left:5,bottom:2),
                            child: Text('Classroom',
                                //textAlign:TextAlign.end,
                                style: TextStyle(
                                color:  Theme.of(context).cursorColor,
                                fontSize: 12.0,
                                height:1.1,
                            )),
                            alignment:  Alignment.bottomLeft,
                          ),
                      ]
                  ),
                  Expanded(
                    child:
                    Container(
                            margin: EdgeInsets.only(right: 15),
                            child: IconButton(
                              icon:  Icon(SFSymbols.search,size: 24,
                                color:  Theme.of(context).primaryTextTheme.caption.color
                                //Color.fromRGBO(118,118,118,1)
                                ,
                              ),
                              onPressed: () {
                                //跳转搜索页
                                RouteTransition(new Search(resourceType:'班级')).pushReplacementRoute(context);
                              },
                              //padding : const EdgeInsets.all(8.0),
                              alignment: Alignment.centerRight,
                            ),
                          ),),
                ]),
              ),
            ),

            Container(
              height: 50,
              color:
              //Theme.of(context).backgroundColor
              Color.fromRGBO(216,216,216,0.5)
              ,
              margin: EdgeInsets.only(left:0.0,top:0),
              child: Align(
                  alignment: Alignment.centerLeft,
                  child:Container(
                      margin: EdgeInsets.only(left:24.0),
                      child:Text('以下是我所在的班级，请在查询中添加新班级信息',style:
                      TextStyle(
                          color:  Theme.of(context).primaryTextTheme.caption.color
                          //Color.fromRGBO(118,118,118,1)
                          ,
                          fontSize: 14.0)))),
            ),
            isEmpty?SizedBox(height: 20):SizedBox(height: 0),
            getClassListView(context),
            SizedBox(height: 20),
            Container(
              alignment: Alignment.center,
              //padding: EdgeInsets.all(16.0),
              child: isEmpty?Text(''):Text(
                '      ******已经到底了******      ',
                style: TextStyle(
                    color:  Theme.of(context).primaryTextTheme.caption.color
                    //Colors.grey
                ),
              ),),
            SizedBox(height: 30),
          ]),)
      ;
  }
  Widget getClassListView(BuildContext context) {
    Widget mainWidget;
    print(countflag.toString());
    print(listData.length.toString());
    //默认空数据状态
    if (listData == [null] || listData.length == 1) {
      if(countflag==1) {
        mainWidget = Center(
          child: CircularProgressIndicator(),
        );
      }
      else{
        mainWidget =                   IntrinsicHeight(child:Align(
          alignment:   Alignment.center,
          child:Column(

            children: <Widget>[
              Expanded(flex: 1,child: SizedBox(height:200),),
              Expanded(flex: 1,child: Container(
                  height:200,
               // margin: EdgeInsets.only(left:24.0),
                child: Text('暂无班级数据',style:
                TextStyle(color:  Theme.of(context).primaryTextTheme.caption.color
                    //Color.fromRGBO(118,118,118,1)
                    ,
                    fontSize: 14.0)),)),
            ],
          )

        ),);
      }
    } else {
      // RefreshIndicator为ListView增加了下拉刷新能力，onRefresh参数传入一个方法，在下拉刷新时调用
      mainWidget =  ListView.builder(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          scrollDirection: Axis.vertical,
          itemCount: listData.length,
          itemBuilder: (bcontext, index) =>
              _renderClassRow(index),
          controller: _controller,
          // physics: AlwaysScrollableScrollPhysics(),

      );
    }
    return mainWidget;
  }

  //初始化项目结构
  Widget _renderClassRow(int index) {
    Widget itemWidget;
    if (listData[index] == null) {
      if (listData.length - 1 == pageNum * curPage) {
        if(countflag==1) {
          //加载更多
          itemWidget = Container(
              padding: const EdgeInsets.all(16.0),
              alignment: Alignment.center,
              child: SizedBox(
                  width: 24.0,
                  height: 24.0,
                  child: CircularProgressIndicator(strokeWidth: 2.0)));
        }
      } else {
        //到头了
        /*return Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(16.0),
            child: Text(
              "----我是有底线的-----",
              style: TextStyle(color: Colors.grey),
            ));*/
      }
    } else {
      itemWidget = Row(
        children: <Widget>[
          Expanded(
            child: getItemWidet(index),
          ),
        ],
      );
    }

    return InkWell(
      child: Padding(
        padding: EdgeInsets.fromLTRB(24, 0, 24, 0),
        child: itemWidget,
      ),
      onTap: () {
        listViewItemFunction(index);
      },
    );
  }
  @override
  Widget getItemWidet(int index) {
      ClassData model = listData[index];
      return Column(
       crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(children: <Widget>[
            Expanded(flex: 8,child:
            Container(
                height: 100,
                color: Colors.white,

                child: Column(children: <Widget>[
                  SizedBox(height: 20),
                  Row(children: <Widget>[
                    Container(
                        height: 25,
                        color: Colors.white,
                        child:Align(
                            alignment: Alignment.centerLeft,
                            child: Text('${model.name.length>15?model.name.substring(0,10)+'...':model.name}',
                                style: TextStyle(
                                    color:  Theme.of(context).primaryTextTheme.caption.color
                                    //Color.fromRGBO(118,118,118,1)
                                    ,
                                    fontSize: 18.0))))
                  ],),
                  SizedBox(height: 10),
                  Row(children: <Widget>[
                    Container(
                        height: 20,
                        color: Colors.white,

                        child:Align(
                            alignment: Alignment.centerLeft,
                            child: Text('${model.type}',style:
                            TextStyle(color:  Theme.of(context).primaryTextTheme.caption.color
                                //Color.fromRGBO(118,118,118,1)
                                ,
                                fontSize: 14.0))))
                  ],),
                  SizedBox(height: 25),
                ],)),
            ),
            Expanded(flex: 1,child: Container(

              color:
              Theme.of(context).backgroundColor
              //Colors.white
              ,
            ),),
            Expanded(flex: 1,child: Container(
              height: 85,
              color:  Theme.of(context).backgroundColor
                //Colors.white
                ,
              child: Align(
              alignment: Alignment.centerRight,
              child:
              Icon(SFSymbols.chevron_right,size: 28,
                color:  Theme.of(context).primaryTextTheme.caption.color
                //Color.fromRGBO(118,118,118,1)
                ,),
              )
              ),)
           ],),

          Divider(
            height: 1,
            color:  Theme.of(context).primaryTextTheme.caption.color
            //Color.fromRGBO(118,118,118,1)
            ,
          ),
        ],
      );
    }

  @override
  void initState() {
    super.initState();
    //initData(false);
    loadMoreData();
  }
  //加载列表数据
  loadMoreData() async {
    var rs =await G.req.classData.getMyClassList();
    setState(() {
      if (rs.data["classList"].length > 0) {
        listData.addAll(
            rs.data["classList"].map((e) => ClassData.fromJson(e)).toList());
        countflag=1;
      }
      else
      {
        countflag=0;
      }
    });
  }
  @override
  void onCreate() {
    // log("onCreate");
    //setAppTopNavBarVisible(true);
    //setListViewItemDel(true);
    //setTopNavTitle('');
  }

  @override
  void onPause() {
    // log('onPause');
  }

  @override
  void onResume() {
    // log("onResume");
  }

  @override
  List<Object> getListData(int currentPageNum)  {
    log("班级页面：onResume");

  }

  @override
  ///ListView Item 单击事件
  void listViewItemFunction(int index)
  {
    ClassData model = listData[index];
    Navigator.of(context).push(new MaterialPageRoute(
        builder: (ctx) => new ClassInfoPage(classId: model.id)));
  }
}
